package arg

import (
	"db2s/ref"
	"fmt"
	"github.com/gurkankaymak/hocon"
	"reflect"
	"strings"
)

func parameterOptions(s any, s1 string) (b bool) {
	switch reflect.TypeOf(s).String() {
	case "map[string]interface {}":
		if _, ok := s.(map[string]any)[s1]; ok {
			return true
		} else {
			return false
		}
	}
	return
}

// checkPar
func checkPar(s SecondaryLevel) (err error) {
	if err = checkDSnsParameter(&s, getDSnsCheck(s.DSnsV)); err != nil {
		return
	}
	if err = ruleParameterCheck(&s, getRulesCheck()); err != nil {
		return
	}
	//if err = schemaParameterCheck(&s, getSchemaCheck()); err != nil {
	//	return
	//}
	if err = repairParameterCheck(&s, getRepairCheck()); err != nil {
		return
	}
	if err = dbPoolParameterCheck(&s, GetDbPoolCheck()); err != nil {
		return
	}
	//if err = logParameterLegitimacy(&s); err != nil {
	//	return
	//}
	return
}

type logLevelLegitimacyVal struct {
	Debug string `json:"debug"`
	Info  string `json:"info"`
	Warn  string `json:"warn"`
	Error string `json:"error"`
}
type logFileLegitimacyVal struct {
	Main     string `json:"main"`
	Schedule string `json:"gt-schedule"`
	SendMsg  string `json:"sendMsg"`
	Monitor  string `json:"monitor"`
	Error    string `json:"error"`
}
type logParameterLegitimacyVal struct {
	LogLevent logLevelLegitimacyVal `json:"level"`
	LogFile   logFileLegitimacyVal
}
type parameterLegitimacyVal struct {
	Log logParameterLegitimacyVal `json:"logLegitimacyVal"`
}

func getLogParameterLegitimacyVal() logParameterLegitimacyVal {
	return logParameterLegitimacyVal{
		LogLevent: logLevelLegitimacyVal{
			Debug: "debug",
			Info:  "info",
			Warn:  "warn",
			Error: "error",
		},
		LogFile: logFileLegitimacyVal{
			Main:     "db2s.log",
			Monitor:  "gt-monitor.log",
			Schedule: "gt-schedule.log",
			SendMsg:  "gt-sendMsg.log",
			Error:    "gt-error.log",
		},
	}
}
func getParameterLegitimacyVal(rr any, key string, val any) (res bool, err error) {
	var qq map[string]any
	qq, err = ref.StructSubJsonNameToMap(rr)
	for subK, subV := range qq {
		if strings.HasPrefix(subK, key) {
			if subV == val {
				res = true
			}
		} else {
			res = true
		}
	}
	return
}
func logParameterLegitimacy(hocon *hocon.Config) (Log logDefVal, err error) {
	if Log, err = logArgParameter(hocon); err != nil {
		return
	}
	var qq = make(map[string]any)
	if qq, err = ref.StructToMap(Log); err != nil {
		return
	}
	for k, v := range qq {
		var exist bool
		if exist, err = getParameterLegitimacyVal(getLogParameterLegitimacyVal(), k, v); err != nil {
			return
		}
		if !exist {
			err = fmt.Errorf("parameter %s is not legitimate", k)
			return
		}
	}
	return
}
